<template>
    <van-address-edit
        :address-info="data.addressInfo"
         show-delete
        show-set-default
        show-search-result
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        :show-area = "false"
        @save="onSave"
    />
</template>

<script setup>
import { userStore } from '@/store/UserStore';
import { useRouter,useRoute } from 'vue-router';
import { onMounted,reactive } from 'vue';
const router = useRouter();
const ustore = userStore();
const route = useRoute();
const data=reactive({
    addressInfo:{}
})

//数据初始化
const init = ()=>{
    ustore.addressList.forEach((item)=>{
        if(item.id == route.query.id){
            data.addressInfo = item;
            data.addressInfo.addressDetail = item.address;
        }
    })
}

onMounted(()=>{
    init();
})


//保存
function onSave(content){
    content.address=content.addressDetail;
    if(route.query.type=='add'){
        ustore.addAddress(content);
        alert('增加成功');
    } else if(route.query.type=='edit'){
        ustore.editAddress(content);
        alert('修改成功');
    }
    
    setTimeout(()=>{
        router.push('./address')
    },1000)
}


</script>

